package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.bean.UserLoginBean;
import com.db.JdbcUtil;

public class UserLoginDao
{

	public int deleteUserLogin(UserLoginBean userLoginBean)
	{
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int mark = 0;
		try
		{
			con = JdbcUtil.getConnection();
			String sql = "delete from lab_userLogin where userID=?";
			ps = con.prepareStatement(sql);
			ps.setString(1, userLoginBean.getUserID());
			mark = ps.executeUpdate();
		}
		catch (Exception e)
		{

		}
		finally
		{
			JdbcUtil.free(rs, ps, con);
		}
		return mark;
	}

	public int updateUserLogin(UserLoginBean userLoginBean)
	{
		Connection con = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		int k = 0;
		try
		{
			con = JdbcUtil.getConnection();
			String sql = "Update lab_userLogin set loginID=?,password=? where userID=?";
			ps = con.prepareStatement(sql);
			ps.setString(1, userLoginBean.getUsername());
			ps.setString(2, userLoginBean.getPassword());
			ps.setString(3, userLoginBean.getUserID());
			k = ps.executeUpdate();

		}
		catch (Exception e)
		{

		}
		finally
		{
			JdbcUtil.free(rs, ps, con);
		}
		return k;

	}

	public ArrayList<UserLoginBean> getUserLogin()
	{
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		ArrayList<UserLoginBean> list = new ArrayList<UserLoginBean>();

		try
		{

			con = JdbcUtil.getConnection();
			String sql = "select userID,loginID,password from lab_userLogin";
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next())
			{
				UserLoginBean userLoginBean = new UserLoginBean();
				userLoginBean.setUsername(rs.getString("loginID"));
				userLoginBean.setUserID(rs.getString("userID"));
				userLoginBean.setPassword(rs.getString("password"));
				list.add(userLoginBean);
			}
		}
		catch (Exception e)
		{
		}
		finally
		{
			JdbcUtil.free(rs, ps, con);
		}
		return list;

	}

	public int getUserLogin(UserLoginBean userLoginBean)
	{
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		ArrayList<UserLoginBean> list = new ArrayList<UserLoginBean>();
		try
		{

			con = JdbcUtil.getConnection();
			String sql = "select *from lab_userLogin where loginID=? and password=?";
			ps = con.prepareStatement(sql);
			ps.setString(1, userLoginBean.getUsername());
			ps.setString(2, userLoginBean.getPassword());
			rs = ps.executeQuery();
			while (rs.next())
			{
				userLoginBean = new UserLoginBean();
				userLoginBean.setUsername(rs.getString("loginID"));
				userLoginBean.setUserID(rs.getString("userID"));
				userLoginBean.setPassword(rs.getString("password"));
				list.add(userLoginBean);
			}
		}
		catch (Exception e)
		{
		}
		finally
		{
			JdbcUtil.free(rs, ps, con);
		}
		return list.size();

	}

}
